<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>销售出库订单信息编辑</title>
</head>
<link rel="stylesheet" type="text/css" href="/wms/js/plugins/jquery-easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="/wms/js/plugins/jquery-easyui/themes/icon.css">
<script type="text/javascript" src="/wms/js/plugins/jquery-easyui/jquery.min.js"></script>
<script type="text/javascript" src="/wms/js/plugins/jquery-easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/wms/js/plugins/jquery-easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="/wms/js/common/common.js"></script>
<script type="text/javascript">
$(function() {
	$(":input[name='stockOutcomeBill.sn']").textbox({});
	$(":input[name='stockOutcomeBill.depot.id']").combobox({
		width:140,
		panelHeight:null
	});
	$(":input[name='stockOutcomeBill.client.id']").combobox({
		width:140,
		panelHeight:null
	});
	$(":input[name='stockOutcomeBill.vdate']").datebox({
		required:true,
		editable:false,
	}).datebox('calendar').calendar({
		validator:function(date) {
			return date <= new Date();
		}
	});
});
</script>
<script type="text/javascript">
	/*添加明细*/
	function addItem() {
		var tr = $("#items_table_body tr:first").clone();
		clearTrData(tr);
		tr.appendTo($("#items_table_body"));
	}
	
	function saveOrUpdate() {
		$.each($("#items_table_body tr"),function(index,item) {
			$(item).find("[tag=pid]").prop("name","stockOutcomeBill.items["+index+"].product.id");
			$(item).find("[tag=salePrice]").prop("name","stockOutcomeBill.items["+index+"].salePrice");
			$(item).find("[tag=number]").prop("name","stockOutcomeBill.items["+index+"].number");
			$(item).find("[tag=remark]").prop("name","stockOutcomeBill.items["+index+"].remark");
		});
		$("#editForm").submit();
	}
	
	/*统一绑定列出货品列表和计算小计的事件 */
	$(function() {
		$("#items_table_body").on("click",".btn_selectProduct",function() {
			$("#productDialog").dialog({
				title:'商品列表', 
				content:'<iframe src="/wms/product_selectProductList.action" width=100% height=99% frameborder=0></iframe>', 
				resizable:true,
				draggable:true,
				width:'80%',
				height:'240%'
			});
			window.tr = $(this).closest("tr");
		}).on("change","[tag=salePrice],[tag=number]",function() {
			var tr = $(this).closest("tr");
			var salePrice = parseFloat(tr.find("[tag=salePrice]").val());
			var number = parseFloat(tr.find("[tag=number]").val());
			if (salePrice && number) {
				var amount = (salePrice * number).toFixed(2);
				tr.find("[tag=amount]").text(amount);
			}
		}).on("click",".btn_removeItem",function() {
			var tr = $(this).closest("tr");
			if ($("#items_table_body tr").length == 1) {
				//清空行
				clearTrData(tr);
			} else {
				//删除行
				tr.remove();
			}
		});
	});
	
	function clearTrData(tr) {
		if (tr) {
			tr.find("[tag=pid]").val("");
			tr.find("[tag=name]").val("");
			tr.find("[tag=brandName]").text("");
			tr.find("[tag=salePrice]").val("");
			tr.find("[tag=number]").val("");
			tr.find("[tag=amount]").text("");
			tr.find("[tag=remark]").val("");
		}
	}
</script>
<body>
	<s:if test="hasActionErrors()">
		<script type="text/javascript">
			$.messager.alert('操作提示','<s:property value="actionErrors[0]"/>','error');
		</script>
	</s:if>
	<form id="editForm" method="POST"
		action="/wms/stockOutcomeBill_saveOrUpdate.action">
		<s:hidden name="stockOutcomeBill.id" />
		<table style="table-layout: fixed;">
			<s:if test="stockOutcomeBill.id != null">
				<tr>
					<td>订单编号</td>
					<td><s:textfield name="stockOutcomeBill.sn" readonly="true"/></td>
				</tr>
			</s:if>
			<tr>
				<td width="70px">出库仓库</td>
				<td><s:select list="#depots" name="stockOutcomeBill.depot.id" listKey="id" listValue="name" /></td>
			</tr>
			<tr>
				<td>销售客户</td>
				<td><s:select list="#clients" name="stockOutcomeBill.client.id" listKey="id" listValue="name" /></td>
			</tr>
			<tr>
				<td>业务时间</td>
				<td>
					<s:date name="stockOutcomeBill.vdate" format="yyyy-MM-dd" var="vd"/>
					<s:textfield name="stockOutcomeBill.vdate" value="%{vd}"/>
				</td>
			</tr>
			<tr>
				<td>明细</td>
				<td><a class="easyui-linkbutton" data-options="text:'添加明细',iconCls:'icon-add',onClick:addItem"></a></td>
			</tr>
			<tr>
				<td></td>
				<td>
					<table border=1 cellspacing="0">
						<thead>
							<tr>
								<th>货品</th>
								<th>品牌</th>
								<th>价格</th>
								<th>数量</th>
								<th>小计</th>
								<th>备注</th>
								<th></th>
							</tr>
						</thead>
						<tbody id="items_table_body">
							 <!-- 新增  -->
							 <s:if test="stockOutcomeBill.id == null">
							 	<tr>
									<td><s:textfield tag="name" /><a class="easyui-linkbutton btn_selectProduct" data-options="text:'选择',iconCls:'icon-search',plain:true"></a>
										<s:hidden tag="pid" />
									</td>
									<td><span tag="brandName"></span></td>
									<td><s:textfield tag="salePrice" /></td>
									<td><s:textfield tag="number" /></td>
									<td><span tag="amount"></span></td>
									<td><s:textfield tag="remark" /></td>
									<td><a class="easyui-linkbutton btn_removeItem" data-options="text:'删除明细',plain:true,iconCls:'icon-remove'"></a></td>
								</tr>
							 </s:if>
							 <!-- 编辑  -->
							<s:else>
								<s:iterator value="stockOutcomeBill.items">
								 	<tr>
										<td><input type="text" value="<s:property value='product.name'/>" tag="name" /><a class="easyui-linkbutton btn_selectProduct" data-options="text:'选择',iconCls:'icon-search',plain:true"></a>
											<s:hidden name="product.id" tag="pid" />
										</td>
										<td><span tag="brandName"><s:property value="product.brand.name"/></span></td>
										<td><input type="text" value="<s:property value='salePrice'/>" tag="salePrice" /></td>
										<td><input type="text" value="<s:property value='number'/>" tag="number" /></td>
										<td><span tag="amount"><s:property value="amount"/></span></td>
										<td><input type="text" value="<s:property value='remark'/>" tag="remark" /></td>
										<td><a class="easyui-linkbutton btn_removeItem" data-options="text:'删除明细',plain:true,iconCls:'icon-remove'"></a></td>
									</tr>
								</s:iterator>
							</s:else>							
						</tbody>
					</table>
				</td>
			</tr>
			<tr>
				<td></td>
				<td>
					<a class="easyui-linkbutton" data-options="text:'保存',iconCls:'icon-ok',onClick:saveOrUpdate"></a>
					<a class="easyui-linkbutton" data-options="text:'取消',iconCls:'icon-cancel',onClick:cancel"></a>
				</td>
			</tr>
		</table>
	</form>
	<div id="productDialog"></div>
</body>
</html>
